package com.lagou.http;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author stevejson
 */
public class Request {
    private String method;
    private String url;
    private InputStream inputStream;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Request(InputStream inputStream) throws IOException {
        this.inputStream = inputStream;
        int count = 0;
        while (count == 0){
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        int read = inputStream.read(bytes);
        String inputString = new String(bytes);
        String firstLine = inputString.split("\\n")[0];
        String[] strings = firstLine.split(" ");
        this.method = strings[0];
        this.url = strings[1];

    }

    public Request() {

    }

    /**
     * @author stevejson
     */
    public static interface Servlet {
        /**
         * 初始化方法
         * @throws Exception ex
         */
        void init() throws Exception;

        /**
         *  销毁
         * @throws Exception ex
         */
        void destroy() throws Exception;

        /**
         *  处理请求
         * @param request re
         * @param response res
         * @throws Exception ex
         */
        void service(Request request, Response response) throws Exception;
    }
}
